/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.properties.datafield;

import javax.xml.namespace.QName;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.fireflow2.designer.eclipse.modelwrapper.basic.DataFieldWrapper;
import org.fireflow2.designer.eclipse.properties.common.DataTypeSelectionDialog;

/**
 * 
 * @author 非也 nychen2000@163.com Fire Workflow 官方网站：www.firesoa.com 或者
 *         www.fireflow.org
 * 
 */
public class DataFieldAttributeEditPanel {
	private DataFieldWrapper modelWrapper = null;
	private Text dataTypeCombo = null;
	private Text initValueText = null;
	private Text idText;
	private Text nameText;
	private Text displayNameText;
	
	public DataFieldAttributeEditPanel(){

	}
	public void setModelWrapper(DataFieldWrapper wrapper){
		modelWrapper = wrapper;
	}
	
	public Control createControl(final Composite composite,
			FormToolkit widgetFactory) {


		composite.setLayout(new GridLayout(3, false));

		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		// id
		Label lb = widgetFactory.createLabel(composite, "Id: ");
		lb.setBackground(composite.getBackground());
		idText = widgetFactory.createText(composite, "");
		idText.setLayoutData(gridData);
		idText.setEditable(false);

		// name
		lb = widgetFactory.createLabel(composite, "名称: ");
		lb.setBackground(composite.getBackground());

		nameText = widgetFactory.createText(composite, "");
		nameText.setLayoutData(gridData);
		nameText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				if (nameText.getText() == null
						|| nameText.getText().trim().equals("")) {
					MessageDialog.openInformation(null, "错误", "名称不能为空！");
					nameText.setFocus();
					return;
				}
				String oldName = modelWrapper.getName();
				if (!oldName.trim().equals(nameText.getText().trim())) {
					if (modelWrapper.isTheNameConflict(nameText.getText()
							.trim())) {
						MessageDialog.openInformation(null, "错误",
								"名称冲突! 名称为 [\"" + nameText.getText()
										+ "\"] 的元素已存在");
						nameText.setFocus();
						return;
					}
					modelWrapper.setName(nameText.getText().trim());
				}
			}

		});

		// displayname
		lb = widgetFactory.createLabel(composite, "显示名称: ");
		lb.setBackground(composite.getBackground());
		
		displayNameText = widgetFactory.createText(composite, "");
		displayNameText.setLayoutData(gridData);
		displayNameText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				modelWrapper.setDisplayName(displayNameText.getText());
			}

		});

		lb = widgetFactory.createLabel(composite, "数据类型: ");
		lb.setBackground(composite.getBackground());
		
		dataTypeCombo = widgetFactory.createText(composite, "");
		dataTypeCombo.setEditable(false);
		dataTypeCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		Button button = widgetFactory.createButton(composite, "...",
				SWT.PUSH);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DataTypeSelectionDialog dialog = new DataTypeSelectionDialog(
						composite.getShell());
				;

				int result = dialog.open();
				if (result == Dialog.OK) {
					QName qName = dialog.getResult();
					modelWrapper.setDataType(qName);
					refresh();
				}

			}

		});

		// CompleteStrategy
		lb = widgetFactory.createLabel(composite, "初始值: ");
		lb.setBackground(composite.getBackground());
		initValueText = widgetFactory.createText(composite, "");
		initValueText.setLayoutData(gridData);
		initValueText.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent arg0) {
				// TODO Auto-generated method stub

			}

			public void focusLost(FocusEvent arg0) {
				// TODO Auto-generated method stub
				String oldValue = (modelWrapper).getInitialValue();
				if (oldValue == null)
					oldValue = "";
				String newValue = initValueText.getText();
				if (newValue == null)
					newValue = "";
				if (oldValue.trim().equals(newValue.trim()))
					return;

				(modelWrapper).setInitialValue(newValue.trim());

			}

		});
		return composite;
	}
	
	public void refresh() {	
		String id = modelWrapper.getID();
		this.idText.setText(id==null?"":id);
		
		String name = modelWrapper.getName();
		this.nameText.setText(name==null?"":name);
		
		String displayName = modelWrapper.getDisplayName();
		this.displayNameText.setText(displayName==null?"":displayName);
		
		String s = (modelWrapper).getInitialValue();
		if (s==null) s="";
		initValueText.setText(s);
		
		QName dataType = modelWrapper.getDataType();
		
//		dataTypeCombo.setText(dataType==null?"":dataType.toString());
		dataTypeCombo.setText(dataType.getPrefix()+":"+dataType.getLocalPart());
	}
	
}
